define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fedition_advertising_roi/index' + location.search,
                    add_url: 'fedition_advertising_roi/add',
                    edit_url: 'fedition_advertising_roi/edit',
                    del_url: 'fedition_advertising_roi/del',
                    multi_url: 'fedition_advertising_roi/multi',
                    import_url: 'fedition_advertising_roi/import',
                    table: 'fedition_advertising_roi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: false,//隐藏分页
                search: false,//隐藏搜索框
                commonSearch: false,//隐藏搜索按钮
                showToggle: false,//表格视图两种模式
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: '展开', operate: false, formatter: Controller.api.formatter.subnode},
                        {field: 'name', title: __('等级'), align: 'left', formatter:function (value, row, index) { return value.toString().replace(/(&|&)nbsp;/g, ' '); } },
                        {field: 'accounturl_name', title: __('店铺'), operate: false},
                        {field: 'advertising_name', title: __('广告账户'), operate: false},

                        {field: 'hourly', title: __('日期或时间段'), operate: false},
                        {field: 'spend', title: __('广告花费'), operate:false},
                        {field: 'sales', title: __('销售额'), operate:false},
                        {field: 'ordershopifycount', title: __('Ordershopifycount')},
                        {field: 'roi', title: __('Roi'), operate: false},
                        {field: 'pid', title: __('Roi'),visible: false, operate: false},

                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新业绩'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('点击更新业绩'),
                                    title: __('点击更新业绩'),
                                    classname: 'btn btn-sm btn-warning btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '等待时间约一分钟，确认开始更新吗？',
                                    url: 'fedition_advertising_roi/edit_status',
                                    visible: function (row) {
                                        if (row.pid == 0) {
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }

                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);

            //当内容渲染完成后
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                //默认隐藏所有子节点
                $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
                // $(".btn-node-sub.disabled").closest("tr").hide();
                //显示隐藏子节点
                $(".btn-node-sub").off("click").on("click", function (e) {
                    var status = $(this).data("shown") ? true : false;
                    $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
                        $(this).closest("tr").toggle(!status);
                    });
                    $(this).data("shown", !status);
                    return false;
                });
                //点击切换/排序/删除操作后刷新左侧菜单
                $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
                    Fast.api.refreshmenu();
                    return false;
                });

            });
            //批量删除后的回调
            $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                Fast.api.refreshmenu();
            });
            //展开隐藏一级
            $(document.body).on("click", ".btn-toggle", function (e) {
                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                var that = this;
                var show = $("i", that).hasClass("fa-chevron-down");
                $("i", that).toggleClass("fa-chevron-down", !show);
                $("i", that).toggleClass("fa-chevron-up", show);
                $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
                $(".btn-node-sub[data-pid=0]").data("shown", show);
            });
            //展开隐藏全部
            $(document.body).on("click", ".btn-toggle-all", function (e) {
                var that = this;
                var show = $("i", that).hasClass("fa-plus");
                $("i", that).toggleClass("fa-plus", !show);
                $("i", that).toggleClass("fa-minus", show);
                $(".btn-node-sub.disabled").closest("tr").toggle(show);
                $(".btn-node-sub").data("shown", show);
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('查看详情') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});